package com.efrei.schoolmanager.model.obj;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;

import com.efrei.schoolmanager.exceptions.LogicException;
import com.efrei.schoolmanager.tools.Verifs;

public class Eleve implements Serializable {
	private static final long serialVersionUID = 2607479767426763706L;
	
	public final static String HOMME = "h";
	public final static String FEMME = "f";
	
	/* Attributs */
	private int eleveId;
	private String matricule;
	private String nom;
	private String prenom;
	private Date dateNaissance;
	private Ville villeNaissance;
	private Pays nationalite;
	private String sexe;
	private String rue;
	private Ville ville;
	private String email;
	private Date dateInscription;
	private Etablissement etablissementPrecedent;
	private String photo;
	private String telDomicile;
	private String telMobile1;
	private String telMobile2;
	private Medecin medecinTraitant;
	private String remarquesMedicales;
	private String vaccinations;
	private String allergies;

	private Set<Responsable> responsablesLegaux = new HashSet<Responsable>();
	private Set<Classe> classes = new HashSet<Classe>();
	private Set<Note> notes = new HashSet<Note>();

	public Eleve() {
	}

	public Eleve(String matricule, String nom, String prenom,
			Date dateNaissance, Ville villeNaissance, Pays nationalite,
			String sexe, String rue, Ville ville, String email,
			Date dateInscription, Etablissement etablissementPrecedent, String photo, String telDomicile,
			String telMobile1, String telMobile2, Medecin medecinTraitant,
			String remarquesMedicales, String vaccinations, String allergies) {
		super();
		this.matricule = matricule;
		this.nom = nom;
		this.prenom = prenom;
		this.dateNaissance = dateNaissance;
		this.villeNaissance = villeNaissance;
		this.nationalite = nationalite;
		this.sexe = sexe;
		this.rue = rue;
		this.ville = ville;
		this.email = email;
		this.dateInscription = dateInscription;
		this.etablissementPrecedent = etablissementPrecedent;
		this.photo = photo;
		this.telDomicile = telDomicile;
		this.telMobile1 = telMobile1;
		this.telMobile2 = telMobile2;
		this.medecinTraitant = medecinTraitant;
		this.remarquesMedicales = remarquesMedicales;
		this.vaccinations = vaccinations;
		this.allergies = allergies;
	}

	@Override
	public String toString() {
		String sep = " - ";
		return new StringBuilder()
			.append(matricule)
			.append(sep).append(nom.toUpperCase())
			.append(" ").append(prenom)
			.toString();
	}

	public int getAge ()
	{
		Calendar today = GregorianCalendar.getInstance();
		GregorianCalendar dateNaissanceCalendar = new GregorianCalendar();
		dateNaissanceCalendar.setTime(dateNaissance);
		
		int age = today.get(Calendar.YEAR)-dateNaissanceCalendar.get(Calendar.YEAR);
		//si on a pas encore dépassé le jour/mois de naissance, faut enlever un an
		//ATTENTION: DAY_OF_YEAR bug avec année bissextile
		if(today.get(Calendar.MONTH) < dateNaissanceCalendar.get(Calendar.MONTH))
			age--;
		else if((today.get(Calendar.MONTH) == dateNaissanceCalendar.get(Calendar.MONTH)) &&
				(today.get(Calendar.DAY_OF_MONTH) > dateNaissanceCalendar.get(Calendar.DAY_OF_MONTH)))
			age--;
		return age;
	}
	
	public Set<Classe> getMostRecentClasses() {
		int promotionMax = -1;
		Set<Classe> classesMax = null;
		for (Classe classe : getClasses()) {
			if(classe.getPromotion()>promotionMax) {
				promotionMax = classe.getPromotion();
				classesMax = new HashSet<Classe>();
				classesMax.add(classe);
			} else if(classe.getPromotion()==promotionMax) {
				classesMax.add(classe);
			}
		}
		return classesMax;
	}
	
	/* getters & setters */

	public int getEleveId() {
		return eleveId;
	}

	public void setEleveId(int eleveId) {
		this.eleveId = eleveId;
	}

	public String getMatricule() {
		return matricule;
	}

	public void setMatricule(String matricule) {
		this.matricule = matricule;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public Date getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(Date dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public Ville getVilleNaissance() {
		return villeNaissance;
	}

	public void setVilleNaissance(Ville villeNaissance) {
		this.villeNaissance = villeNaissance;
	}

	public Pays getNationalite() {
		return nationalite;
	}

	public void setNationalite(Pays nationalite) {
		this.nationalite = nationalite;
	}

	public String getSexe() {
		return sexe;
	}

	public void setSexe(String sexe) throws IllegalArgumentException {
		if(!sexe.equals(HOMME) && !sexe.equals(FEMME))
			throw new IllegalArgumentException("h/f attendu, '"+sexe+"' obtenu !");
		this.sexe = sexe;
	}

	public String getRue() {
		return rue;
	}

	public void setRue(String rue) {
		this.rue = rue;
	}

	public Ville getVille() {
		return ville;
	}

	public void setVille(Ville ville) {
		this.ville = ville;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getDateInscription() {
		return dateInscription;
	}

	public void setDateInscription(Date dateInscription) {
		this.dateInscription = dateInscription;
	}

	public Etablissement getEtablissementPrecedent() {
		return etablissementPrecedent;
	}

	public void setEtablissementPrecedent(Etablissement etablissementPrecedent) {
		this.etablissementPrecedent = etablissementPrecedent;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public String getTelDomicile() {
		return telDomicile;
	}

	public void setTelDomicile(String telDomicile) {
		this.telDomicile = telDomicile;
	}

	public String getTelMobile1() {
		return telMobile1;
	}

	public void setTelMobile1(String telMobile1) {
		this.telMobile1 = telMobile1;
	}

	public String getTelMobile2() {
		return telMobile2;
	}

	public void setTelMobile2(String telMobile2) {
		this.telMobile2 = telMobile2;
	}

	public Medecin getMedecinTraitant() {
		return medecinTraitant;
	}

	public void setMedecinTraitant(Medecin medecinTraitant) {
		this.medecinTraitant = medecinTraitant;
	}

	public String getRemarquesMedicales() {
		return remarquesMedicales;
	}

	public void setRemarquesMedicales(String remarquesMedicales) {
		this.remarquesMedicales = remarquesMedicales;
	}

	public String getVaccinations() {
		return vaccinations;
	}

	public void setVaccinations(String vaccinations) {
		this.vaccinations = vaccinations;
	}

	public String getAllergies() {
		return allergies;
	}

	public void setAllergies(String allergies) {
		this.allergies = allergies;
	}

	public Set<Responsable> getResponsablesLegaux() {
		return responsablesLegaux;
	}

	public void setResponsablesLegaux(Set<Responsable> responsablesLegaux) {
		this.responsablesLegaux = responsablesLegaux;
	}

	public Set<Classe> getClasses() {
		return classes;
	}

	public void setClasses(Set<Classe> classes) {
		this.classes = classes;
	}
	
	public Set<Note> getNotes() {
		return notes;
	}
	
	public void setNotes(Set<Note> notes) {
		this.notes = notes;
	}
	
	/**
	 * verifie les champs String pour l'eleve
	 * @param matricule
	 * @param nom
	 * @param prenom
	 * @param sexe
	 * @param rue
	 * @param email
	 * @param photo
	 * @param telDomicile
	 * @param telMobile1
	 * @param telMobile2
	 * @param remarquesMedicales
	 * @param vaccinations
	 * @param allergies
	 * @throws LogicException
	 */
	public static void check(String matricule, String nom, String prenom,
			String sexe, String rue, String email,
			String photo, String telDomicile,
			String telMobile1, String telMobile2, String remarquesMedicales,
			String vaccinations, String allergies) throws LogicException {
		Verifs.checkString(matricule, "matricule invalide");
		Verifs.checkString(nom, "nom invalide");
		Verifs.checkString(prenom, "prenom invalide");
		Verifs.checkSexe(sexe);
		Verifs.checkString(rue, "rue invalide");
		Verifs.checkNullString(photo, "photo invalide");
		Verifs.checkNullString(telDomicile, "telephone domicile invalide");
		Verifs.checkNullString(telMobile1, "telephone mobile invalide");
		Verifs.checkNullString(telMobile2, "telephone mobile 2 invalide");
		Verifs.checkNullString(email, "email invalide");
		Verifs.checkNullString(remarquesMedicales, "remarques medicales invalide(s)");
		Verifs.checkNullString(vaccinations, "vaccinations invalide(s)");
		Verifs.checkNullString(allergies, "allergies invalide(s)");
	}
}

